﻿Imports System.Data.SqlClient
Imports Mysoft.Map.Data
Imports System.Text
Imports System.Xml
Imports System.IO
Imports Mysoft.Map.Workflow.Public

Partial Public Class WF_ProcessDefined_Print
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            If Not Request.QueryString("oid") Is Nothing Then
                Dim PMGUID As String = Request.QueryString("oid").ToString()
                Dim conn As New SqlConnection(MyDB.GetSqlConnectionString())
                Dim dadp As New SqlClient.SqlDataAdapter()

                dadp.SelectCommand = New SqlCommand()
                dadp.SelectCommand.CommandText = "usp_myWorkflow_GetProcessModuleXml"

                dadp.SelectCommand.Parameters.Add("@PMGUID", SqlDbType.VarChar, 36)
                dadp.SelectCommand.Parameters("@PMGUID").Value = PMGUID

                dadp.SelectCommand.CommandType = CommandType.StoredProcedure
                dadp.SelectCommand.Connection = conn

                Dim ds As DataSet = New DataSet("pminfo")
                Try
                    conn.Open()
                    dadp.Fill(ds)
                    conn.Close()
                Catch ex As Exception
                    If conn.State = ConnectionState.Open Then
                        conn.Close()
                    End If
                    Throw ex
                End Try
            Dim xmlDoc As New XmlDocument
            Dim xslt As New Xsl.XslTransform

            ds.Tables(0).TableName = "pm"
            ds.Tables(1).TableName = "sm"
            ds.Tables(2).TableName = "bm"
            ds.Tables(3).TableName = "rm"
            ds.Tables(4).TableName = "cc"

            '加入列归档责任人列
            Dim newColumn As New DataColumn("AuditorName")
            ds.Tables("pm").Columns.Add(newColumn)

                Dim auditorName As String = String.Empty
            For Each row As DataRow In ds.Tables("bm").Select("StepType=3")
                auditorName &= (row("AuditorName") & ",")
            Next
            ds.Tables("pm").Rows(0)("AuditorName") = RemoveLast(auditorName, 1)

            '加入步骤责任人，流转信息，抄送人列
            Dim newColumn1 As New DataColumn("AuditorName")
            Dim newColumn2 As New DataColumn("RelationInfo")
            Dim newColumn3 As New DataColumn("CCAuditorName")
            ds.Tables("sm").Columns.Add(newColumn1)
            ds.Tables("sm").Columns.Add(newColumn2)
            ds.Tables("sm").Columns.Add(newColumn3)

            For Each smrow As DataRow In ds.Tables("sm").Rows
                smrow("AuditorName") = ""
                smrow("RelationInfo") = ""
                smrow("CCAuditorName") = ""
                For Each bmrow As DataRow In ds.Tables("bm").Select("StepId=" & smrow("StepId"))
                    smrow("AuditorName") &= (bmrow("AuditorName") & ",")
                Next
                smrow("AuditorName") = RemoveLast(smrow("AuditorName"), 1)

                For Each rmrow As DataRow In ds.Tables("rm").Select("StepId=" & smrow("StepId"))
                    smrow("RelationInfo") &= String.Format("当&lt;label class=&#39;s1&#39;&gt;{0}&lt;/label&gt;时，流向步骤{1}&lt;br/&gt;", rmrow("Expression"), rmrow("NextStepGUID"))
                Next
                smrow("RelationInfo") = RemoveLast(smrow("RelationInfo"), 11)

                For Each ccrow As DataRow In ds.Tables("cc").Select("StepId=" & smrow("StepId"))
                    smrow("CCAuditorName") &= (ccrow("AuditorName") & ",")
                Next
                smrow("CCAuditorName") = RemoveLast(smrow("CCAuditorName"), 1)
            Next

            Dim sbXml As New StringBuilder("<pm><baseinfo>")
            For Each column As DataColumn In ds.Tables("pm").Columns
                sbXml.AppendFormat("<{0}>{1}</{0}>", column.ColumnName, ds.Tables("pm").Rows(0)(column))
            Next
            sbXml.Append("</baseinfo><steps>")

            For Each row As DataRow In ds.Tables("sm").Rows
                sbXml.Append("<step>")
                For Each col As DataColumn In ds.Tables("sm").Columns
                    sbXml.AppendFormat("<{0}>{1}</{0}>", col.ColumnName, row(col))
                Next
                sbXml.Append("</step>")
            Next

            sbXml.Append("</steps>")

            sbXml.Append("</pm>")

            xmlDoc.LoadXml(sbXml.ToString())

                'xslt.Load(Server.MapPath("/MyWorkflow/XSLT/WF_ProcessDefined_Print.xslt"))
                xslt = WFCache.GetXsltFile("/MyWorkflow/XSLT/WF_ProcessDefined_Print.xslt")

            Dim output As New StringBuilder("")
            Dim writer As New StringWriter(output)

            xslt.Transform(xmlDoc, Nothing, writer)
            liContent.Text = output.ToString()
        End If
        End If
    End Sub

    '移除末尾指定长度字符
    Private Function RemoveLast(ByVal str As String, ByVal len As Integer) As String
        If str.Length < len Then
            Return ""
        Else
            Return str.Remove(str.Length - len, len)
        End If
    End Function

End Class